package com.cmc6.param.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.model.dto.NotNullId;
import com.cmc6.common.model.entity.ParamDO;
import com.cmc6.common.util.ApiResult;
import com.cmc6.param.model.dto.ParamInsertOrUpdateDTO;
import com.cmc6.param.model.dto.ParamPageDTO;
import com.cmc6.param.service.ParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 注意：如果这里增加了修改 value的方法，这里需要进行同步修改 {@link com.cmc6.common.constant.BaseConstant#PARAM_CHANGE_VALUE_URI_SET}
 */
@RequestMapping("/param")
@RestController
@Api(tags = "系统参数配置")
public class ParamController {

    @Resource
    ParamService baseService;

    @ApiOperation(value = "新增/修改 系统参数")
    @PostMapping("/insertOrUpdate")
    @PreAuthorize("hasAuthority('param:insertOrUpdate')")
    public ApiResult<String> insertOrUpdate(@RequestBody @Valid ParamInsertOrUpdateDTO dto) {
        return ApiResult.ok(baseService.insertOrUpdate(dto));
    }

    @ApiOperation(value = "分页排序查询：系统参数")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('param:page')")
    public ApiResult<Page<ParamDO>> myPage(@RequestBody @Valid ParamPageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @ApiOperation(value = "通过主键id，查看详情")
    @PostMapping("/infoById")
    @PreAuthorize("hasAuthority('param:infoById')")
    public ApiResult<ParamDO> infoById(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.infoById(notNullId));
    }

    @ApiOperation(value = "删除系统参数")
    @PostMapping("/deleteByIdSet")
    @PreAuthorize("hasAuthority('param:deleteByIdSet')")
    public ApiResult<String> deleteByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.deleteByIdSet(notEmptyIdSet));
    }

}
